
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>权限验证 · introduce</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Gj">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../index.css">
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="style.html" />
    
    
    <link rel="prev" href="eslint.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    引导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="vscode.html">
            
                <a href="vscode.html">
            
                    
                    Vscode
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="jsdoc.html">
            
                <a href="jsdoc.html">
            
                    
                    Jsdoc
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="webpack.html">
            
                <a href="webpack.html">
            
                    
                    Webpack
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="router.html">
            
                <a href="router.html">
            
                    
                    路由和侧边栏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="env.html">
            
                <a href="env.html">
            
                    
                    环境变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="cors.html">
            
                <a href="cors.html">
            
                    
                    跨域问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="router-lazyload.html">
            
                <a href="router-lazyload.html">
            
                    
                    路由懒加载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="icons.html">
            
                <a href="icons.html">
            
                    
                    图标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="i18n.html">
            
                <a href="i18n.html">
            
                    
                    国际化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="eslint.html">
            
                <a href="eslint.html">
            
                    
                    ESLint
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.12" data-path="permission.html">
            
                <a href="permission.html">
            
                    
                    权限验证
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="style.html">
            
                <a href="style.html">
            
                    
                    样式
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >权限验证</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6743;&#x9650;&#x9A8C;&#x8BC1;">&#x6743;&#x9650;&#x9A8C;&#x8BC1;</h1>
<p>&#x8BE5;&#x9879;&#x76EE;&#x4E2D;&#x6743;&#x9650;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x662F;&#xFF1A;&#x901A;&#x8FC7;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7528;&#x6237;&#x7684;&#x6743;&#x9650;&#x53BB;&#x6BD4;&#x5BF9;&#x8DEF;&#x7531;&#x8868;&#xFF0C;&#x751F;&#x6210;&#x5F53;&#x524D;&#x7528;&#x6237;&#x5177;&#x7684;&#x6743;&#x9650;&#x53EF;&#x8BBF;&#x95EE;&#x7684;&#x8DEF;&#x7531;&#x8868;&#xFF0C;&#x901A;&#x8FC7; <code>router.addRoutes</code> &#x52A8;&#x6001;&#x6302;&#x8F7D;&#x5230; <code>router</code> &#x4E0A;&#x3002;</p>
<p>&#x4F46;&#x5176;&#x5B9E;&#x5F88;&#x591A;&#x516C;&#x53F8;&#x7684;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x53EF;&#x80FD;&#x4E0D;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF0C;&#x4E3E;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x6765;&#x8BF4;&#xFF0C;&#x5F88;&#x591A;&#x516C;&#x53F8;&#x7684;&#x9700;&#x6C42;&#x662F;&#x6BCF;&#x4E2A;&#x9875;&#x9762;&#x7684;&#x6743;&#x9650;&#x662F;&#x52A8;&#x6001;&#x914D;&#x7F6E;&#x7684;&#xFF0C;&#x4E0D;&#x50CF;&#x672C;&#x9879;&#x76EE;&#x4E2D;&#x662F;&#x5199;&#x6B7B;&#x9884;&#x8BBE;&#x7684;&#x3002;&#x4F46;&#x5176;&#x5B9E;&#x539F;&#x7406;&#x662F;&#x76F8;&#x540C;&#x7684;&#x3002;&#x5982;&#xFF1A;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x540E;&#x53F0;&#x901A;&#x8FC7;&#x4E00;&#x4E2A; tree &#x63A7;&#x4EF6;&#x6216;&#x8005;&#x5176;&#x5B83;&#x5C55;&#x73B0;&#x5F62;&#x5F0F;&#x7ED9;&#x6BCF;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x52A8;&#x6001;&#x914D;&#x7F6E;&#x6743;&#x9650;&#xFF0C;&#x4E4B;&#x540E;&#x5C06;&#x8FD9;&#x4EFD;&#x8DEF;&#x7531;&#x8868;&#x5B58;&#x50A8;&#x5230;&#x540E;&#x7AEF;&#x3002;&#x5F53;&#x7528;&#x6237;&#x767B;&#x5F55;&#x540E;&#x5F97;&#x5230; <code>roles</code>&#xFF0C;&#x524D;&#x7AEF;&#x6839;&#x636E;<code>roles</code> &#x53BB;&#x5411;&#x540E;&#x7AEF;&#x8BF7;&#x6C42;&#x53EF;&#x8BBF;&#x95EE;&#x7684;&#x8DEF;&#x7531;&#x8868;&#xFF0C;&#x4ECE;&#x800C;&#x52A8;&#x6001;&#x751F;&#x6210;&#x53EF;&#x8BBF;&#x95EE;&#x9875;&#x9762;&#xFF0C;&#x4E4B;&#x540E;&#x5C31;&#x662F; router.addRoutes &#x52A8;&#x6001;&#x6302;&#x8F7D;&#x5230; router &#x4E0A;&#xFF0C;&#x4F60;&#x4F1A;&#x53D1;&#x73B0;&#x539F;&#x6765;&#x662F;&#x76F8;&#x540C;&#x7684;&#xFF0C;&#x4E07;&#x53D8;&#x4E0D;&#x79BB;&#x5176;&#x5B97;&#x3002;</p>
<p>&#x53EA;&#x662F;&#x591A;&#x4E86;&#x4E00;&#x6B65;&#x5C06;&#x540E;&#x7AEF;&#x8FD4;&#x56DE;&#x8DEF;&#x7531;&#x8868;&#x548C;&#x672C;&#x5730;&#x7684;&#x7EC4;&#x4EF6;&#x6620;&#x5C04;&#x5230;&#x4E00;&#x8D77;&#x3002;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">const</span> map<span class="token operator">=</span><span class="token punctuation">{</span>
 login<span class="token operator">:</span><span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&apos;login/index&apos;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>default <span class="token comment">// &#x540C;&#x6B65;&#x7684;&#x65B9;&#x5F0F;</span>
 <span class="token function-variable function">login</span><span class="token operator">:</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">&apos;login/index&apos;</span><span class="token punctuation">)</span>      <span class="token comment">// &#x5F02;&#x6B65;&#x7684;&#x65B9;&#x5F0F;</span>
<span class="token punctuation">}</span>
<span class="token comment">//&#x4F60;&#x5B58;&#x5728;&#x670D;&#x52A1;&#x7AEF;&#x7684;map&#x7C7B;&#x4F3C;&#x4E8E;</span>
<span class="token keyword">const</span> serviceMap<span class="token operator">=</span><span class="token punctuation">[</span>
 <span class="token punctuation">{</span> path<span class="token operator">:</span> <span class="token string">&apos;/login&apos;</span><span class="token punctuation">,</span> component<span class="token operator">:</span> <span class="token string">&apos;login&apos;</span><span class="token punctuation">,</span> hidden<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token comment">//&#x4E4B;&#x540E;&#x904D;&#x5386;&#x8FD9;&#x4E2A;map&#xFF0C;&#x52A8;&#x6001;&#x751F;&#x6210;asyncRoutes</span>
&#x5E76;&#x5C06; component &#x66FF;&#x6362;&#x4E3A; map<span class="token punctuation">[</span>component<span class="token punctuation">]</span>
</code></pre>
<p>Ps:&#x4E0D;&#x6392;&#x9664;&#x4E4B;&#x540E;&#x672C;&#x9879;&#x76EE;&#x4F1A;&#x589E;&#x52A0;&#x6743;&#x9650;&#x63A7;&#x5236;&#x9762;&#x677F;&#x652F;&#x6301;&#x771F;&#x6B63;&#x7684;&#x52A8;&#x6001;&#x914D;&#x7F6E;&#x6743;&#x9650;&#x3002;</p>
<h2 id="&#x903B;&#x8F91;&#x4FEE;&#x6539;">&#x903B;&#x8F91;&#x4FEE;&#x6539;</h2>
<p>&#x73B0;&#x5728;&#x8DEF;&#x7531;&#x5C42;&#x9762;&#x6743;&#x9650;&#x7684;&#x63A7;&#x5236;&#x4EE3;&#x7801;&#x90FD;&#x5728; <code>@/permission.js</code> &#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x4FEE;&#x6539;&#x903B;&#x8F91;&#xFF0C;&#x76F4;&#x63A5;&#x5728;&#x9002;&#x5F53;&#x7684;&#x5224;&#x65AD;&#x903B;&#x8F91;&#x4E2D; <code>next()</code> &#x91CA;&#x653E;&#x94A9;&#x5B50;&#x5373;&#x53EF;&#x3002;</p>
<h2 id="&#x6307;&#x4EE4;&#x6743;&#x9650;">&#x6307;&#x4EE4;&#x6743;&#x9650;</h2>
<p>&#x5C01;&#x88C5;&#x4E86;&#x4E00;&#x4E2A;&#x6307;&#x4EE4;&#x6743;&#x9650;&#xFF0C;&#x80FD;&#x7B80;&#x5355;&#x5FEB;&#x901F;&#x7684;&#x5B9E;&#x73B0;&#x6309;&#x94AE;&#x7EA7;&#x522B;&#x7684;&#x6743;&#x9650;&#x5224;&#x65AD;&#x3002; <a href="https://github.com/PanJiaChen/vue-element-admin/tree/master/src/directive/permission" target="_blank">v-permission</a></p>
<p><strong>&#x4F7F;&#x7528;</strong></p>
<pre class="language-"><code class="lang-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token comment">&lt;!-- Admin can see this --&gt;</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-tag</span> <span class="token attr-name">v-permission</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>[&apos;admin&apos;]<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>admin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-tag</span><span class="token punctuation">&gt;</span></span>

  <span class="token comment">&lt;!-- Editor can see this --&gt;</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-tag</span> <span class="token attr-name">v-permission</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>[&apos;editor&apos;]<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>editor<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-tag</span><span class="token punctuation">&gt;</span></span>

  <span class="token comment">&lt;!-- Editor can see this --&gt;</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-tag</span> <span class="token attr-name">v-permission</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>[&apos;admin&apos;,&apos;editor&apos;]<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Both admin or editor can see this<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-tag</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span>
// &#x5F53;&#x7136;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x4F7F;&#x7528;&#xFF0C;&#x5C06;&#x5B83;&#x6CE8;&#x518C;&#x5230;&#x5168;&#x5C40;
import permission from &apos;@/directive/permission/index.js&apos; // &#x6743;&#x9650;&#x5224;&#x65AD;&#x6307;&#x4EE4;
export default{
  directives: { permission }
}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<p><strong>&#x5C40;&#x9650;</strong></p>
<p>In some cases it is not suitable to use v-permission, such as element Tab component which can only be achieved by manually setting the v-if.</p>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5168;&#x5C40;&#x6743;&#x9650;&#x5224;&#x65AD;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x6CD5;&#x548C;&#x6307;&#x4EE4; <code>v-permission</code> &#x7C7B;&#x4F3C;&#x3002;</p>
<pre class="language-"><code class="lang-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-tab-pane</span> <span class="token attr-name">v-if</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>checkPermission([&apos;admin&apos;])<span class="token punctuation">&quot;</span></span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>Admin<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Admin can see this<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-tab-pane</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-tab-pane</span> <span class="token attr-name">v-if</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>checkPermission([&apos;editor&apos;])<span class="token punctuation">&quot;</span></span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>Editor<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Editor can see this<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-tab-pane</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-tab-pane</span> <span class="token attr-name">v-if</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>checkPermission([&apos;admin&apos;,&apos;editor&apos;])<span class="token punctuation">&quot;</span></span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>Admin-OR-Editor<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Both admin or editor can see this<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-tab-pane</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span>
import checkPermission from &apos;@/utils/permission&apos; // &#x6743;&#x9650;&#x5224;&#x65AD;&#x51FD;&#x6570;

export default{
   methods: {
    checkPermission
   }
}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="eslint.html" class="navigation navigation-prev " aria-label="Previous page: ESLint">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="style.html" class="navigation navigation-next " aria-label="Next page: 样式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"权限验证","level":"1.12","depth":1,"next":{"title":"样式","level":"1.13","depth":1,"path":"src/style.md","ref":"src/style.md","articles":[]},"previous":{"title":"ESLint","level":"1.11","depth":1,"path":"src/eslint.md","ref":"src/eslint.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","-lunr","-search","search-pro","-highlight","prism","livereload"],"styles":{"website":"index.css"},"pluginsConfig":{"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"expandable-chapters":{},"search-pro":{},"livereload":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Gj","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"introduce","language":"zh-hans","gitbook":"3.2.3","description":"项目搭建、规范、使用示例介绍"},"file":{"path":"src/permission.md","mtime":"2020-07-29T03:24:01.132Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-29T07:02:36.580Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

